#include "playscene.h"
#include "chooselevelscene.h"
#include "mypushbutton.h"
#include "mymenubar.h"
#include <QDebug>
#include <QString>
#include <QPainter>
#include <QAction>
#include <QMenuBar>
#include <QPaintEvent>
#include <QLabel>
#include <QFont>
#include <QTimer>
#include <QPropertyAnimation> //动画头文件





PlayScene::PlayScene(int levelNum)
{
    QString str = QString("第%1关").arg(levelNum);

    this->levelIndex = levelNum;  //记录关卡号

    //设置大小和标题
    this->setFixedSize(320,580);
    this->setWindowIcon(QPixmap(":/resource/image/Coin0001.png"));
    this->setWindowTitle(str);

    //菜单栏创建
    CreateMenuBar();



    //back 按钮实现
    MyPushButton * backBtn = new MyPushButton(":/resource/image/BackButton.png");
    backBtn->setParent(this);
    backBtn->move(this->width() - backBtn->width(), this->height() -  backBtn->height()); //放在右下角
    connect(backBtn,&MyPushButton::clicked, this, [=](){
        emit this->playSceneBack();  //发送返回信号告知主窗口

    });

    //显示关卡号
    QLabel * levelLabel = new QLabel;
    levelLabel->setParent(this);
    QFont levelFont;
    levelFont.setFamily("华文新魏");
    levelFont.setPointSize(20);
    QString leveIndexStr = QString("Level: %1").arg(this->levelIndex);
    levelLabel->setFont(levelFont);  //应用字体
    levelLabel->setText(leveIndexStr);
    levelLabel->setGeometry(30, this->height()-50, 170, 50);

    //初始化配置数据
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            this->initArray[i][j] = config.mData[this->levelIndex][i][j];
        }
    }

    //胜利图片懒羊羊创建
    QLabel *winLabel = new QLabel;
    winLabel->setParent(this);
    QPixmap tmpPix;
    tmpPix.load(":/resource/image/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0, 0, tmpPix.width(), tmpPix.height());
    winLabel->setPixmap(tmpPix);
    winLabel->move((this->width()-tmpPix.width())*0.5, -tmpPix.height());  // -tmpPix.height()：这里y坐标取负值，图片显示在窗口之外，看不见，等胜利后再弹下来

    //显示金币矩阵的背景图片
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            QPixmap pix = QPixmap(":/resource/image/BoardNode.png");
            QLabel * baLabel = new QLabel;
            baLabel->setParent(this);
            baLabel->setGeometry(0,0,50,50);
            baLabel->setPixmap(pix);
            baLabel->move(((this->width()/2) - 50*2) + j*50, ((this->height()/2) - 50*2)+ i*50);// 计算窗体中间位置

            //显示矩阵金币
            QString cfgStr;
            if (1 == this->initArray[i][j])
            {
                cfgStr = ":/resource/image/Coin0001.png";  //是1就显示金币
            }
            else
            {
                cfgStr = ":/resource/image/Coin0008.png";   //不是1就显示银币
            }
            MyCoin *coin = new MyCoin(cfgStr);
            coin->setParent(this);
            coin->move((((this->width()/2) - 50*2) + j*50)+2, ((this->height()/2) - 50*2)+ i*50);//为了对齐，x方向多偏移2个像素

            //给金币赋值
            coin->posX = i;
            coin->posY = j;
            coin->flag = this->initArray[i][j];
            coinArray[i][j] = coin;             //维护当前位置的金币

            //关注鼠标的点击，开始翻转
            connect(coin, &MyCoin::clicked, this, [=](){
                {
                    //禁用其他金币的点击
                    for(int i = 0; i < 4; i++)
                    {
                        for(int j = 0; j < 4; j++)
                        {
                            this->coinArray[i][j]->isCoinWin = true;    //禁用其他金币的点击
                        }
                    }


                    coin->changeFlag(); //翻转金币
                    this->initArray[i][j] = this->initArray[i][j] == 0?1:0;  //三目运算符，维护initArray数组

                    //周围的金币跟着翻转， 延时300ms再翻转周围的金币
                    QTimer::singleShot(300, this,[=](){
                        //左边金币
                        if (coin->posX-1 >= 0)
                        {
                            coinArray[coin->posX-1][coin->posY]->changeFlag();
                            this->initArray[coin->posX-1][coin->posY] = this->initArray[coin->posX-1][coin->posY] == 0?1:0;
                        }
                        //右边金币
                        if (coin->posX+1 <= 3)
                        {
                            coinArray[coin->posX+1][coin->posY]->changeFlag();
                            this->initArray[coin->posX+1][coin->posY] = this->initArray[coin->posX+1][coin->posY] == 0?1:0;
                        }
                        //上边金币
                        if (coin->posY-1 >= 0)
                        {
                            coinArray[coin->posX][coin->posY-1]->changeFlag();
                            this->initArray[coin->posX][coin->posY-1] = this->initArray[coin->posX][coin->posY-1] == 0?1:0;
                        }
                        //下边金币
                        if (coin->posY+1 <= 3)
                        {
                            coinArray[coin->posX][coin->posY+1]->changeFlag();
                            this->initArray[coin->posX][coin->posY+1] = this->initArray[coin->posX][coin->posY+1] == 0?1:0;
                        }

                        //解开禁用其他金币的点击
                        for(int i = 0; i < 4; i++)
                        {
                            for(int j = 0; j < 4; j++)
                            {
                                this->coinArray[i][j]->isCoinWin = false;    //解开禁用点击其他金币
                            }
                        }

                        //判断是否胜利
                        this->isWin = true;
                        for (int i = 0; i < 4; i++)
                        {
                            for(int j = 0; j < 4; j++)
                            {
                                if (false == coinArray[i][j]->flag)  //只要有一个是反面就是失败
                                {
                                    this->isWin = false;
                                    break;
                                }
                            }
                        }

                        if (true == this->isWin)
                        {
                            for(int i = 0; i < 4; i++)
                            {
                                for(int j = 0; j < 4; j++)
                                {
                                    coinArray[i][j]->isCoinWin = true;  //所以按钮的胜利标志改为 true
                                }
                            }

                            //弹出懒羊羊胜利图片
                            QPropertyAnimation * animation = new QPropertyAnimation(winLabel, "geometry");
                            animation->setDuration(1000); //时间间隔
                            animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                            animation->setEndValue(QRect(winLabel->x(),winLabel->y()+130,winLabel->width(),winLabel->height()));
                            animation->setEasingCurve(QEasingCurve::OutBounce); //设置动画
                            animation->start();
                        }

                    });

                };
            });

        }
    }

}

//设置关卡窗口的背景
void PlayScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/resource/image/PlayLevelSceneBg.png");
    painter.drawPixmap(0, 0, this->width(), this->height(), pix);  //指定背景图宽度和高度为窗体一样

    //标题图标设置
    pix.load(":/resource/image/Title.png");
    pix = pix.scaled(pix.width() * 0.5, pix.height()*0.5);  //缩放图标的宽和高都为为原来的0.5倍
    painter.drawPixmap(10, 30, pix);
}

